//
//  MOMyGetMarriedProjectEditOrCreatViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/8/16.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOMyGetMarriedProjectEditOrCreatViewController.h"
#import "MOMyGetMarriedProjectEditOrCreatViewCell.h"
#import "MOMyGetMarriedAddPeopleView.h"
#import "MOMyGetMarriedAddPeopleModel.h"

@interface MOMyGetMarriedProjectEditOrCreatViewController ()<UITableViewDelegate,UITableViewDataSource,XLsn0wPickerTimerDelegate>

@property(nonatomic,strong)UIView *navView;

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataSource;

@property(nonatomic,strong)UIView *bottomView;

@property(nonatomic,strong)NSMutableArray *familyPeopleArray;

@property(nonatomic,strong)NSString *masterId;

@end

@implementation MOMyGetMarriedProjectEditOrCreatViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatView];
    
    [self initData];
    
    if ([self.editOrCreatStatus integerValue]==1) {
        [self getData];
    }
}

-(void)creatView
{
    self.view.backgroundColor = KGrayColor_ededed;
    
    [self.view addSubview:self.navView];
    
    [self.view addSubview:self.tableView];
    
    [self.view addSubview:self.bottomView];
    
//    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
//    view.backgroundColor = [UIColor whiteColor];
//    self.tableView.tableFooterView = view;
}

-(void)initData
{
    self.familyPeopleArray = [NSMutableArray array];
    
    NSArray *arr = @[@"户主姓名",@"性别",@"族别",@"出生年月",@"文化程度",@"政治面貌",@"职业",@"身份",@"宗教信仰",@"健康状况",@"身份证号",@"联系方式",@"家庭人口",@"村子名称",@"家庭年收入",@"主要经济来源",@"是否流动人口",@"流出地",@"家庭住址"];
    NSArray *pArr = @[@"请填写户主姓名",@"请选择性别",@"请选择族别",@"请选择出生年月",@"请填写文化程度",@"请填写政治面貌",@"请填写职业",@"请填写身份",@"请填写宗教信仰",@"请选择健康状况",@"请填写身份证号",@"请填写联系方式",@"请填写家庭人口数量",@"请填写村子名称",@"请填写家庭年收入",@"请填写主要经济来源",@"请选择",@"请填写流出地",@"请填写家庭住址"];
    NSArray *tArr = @[@0,@0,@0,@0,@0,@0,@0,@0,@0,@0,@0,@4,@4,@0,@8,@0,@0,@0,@0];
    self.dataSource = [NSMutableArray array];
    for (int i = 0; i<arr.count; i++) {
        MOMyGetMarriedProjectEditOrCreatViewModel *model = [[MOMyGetMarriedProjectEditOrCreatViewModel alloc]init];
        model.nameStr = arr[i];
        model.placeStr = pArr[i];
        model.labelWidth = 100;
        model.KeyboardType = [tArr[i] integerValue];
        if (i<arr.count-2) {
            if (i==1||i==2||i==3||i==4||i==5||i==8||i==9||i==16) {
                model.layoutType = @"3";
            }else{
                model.layoutType = @"1";
            }
        }else{
            model.layoutType = @"2";
        }
        [self.dataSource addObject:model];
    }
    [self.tableView reloadData];
}

-(void)getData
{
    NSString *url = [NSString stringWithFormat:@"%@?userId=%@&authorization=%@",RECIPIENT_GETRECIPIENTSBYID_URL,[MOUser sharedMOUser].userName,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkHelper POST:url parameters:@{@"masterId":self.projectDic[@"masterId"]} success:^(id responseObject) {
        NSLog(@"结亲对象详情：%@",responseObject);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            
            self.masterId = responseObject[@"masterId"];
            for (int i = 0; i<self.dataSource.count; i++) {
                MOMyGetMarriedProjectEditOrCreatViewModel *model = self.dataSource[i];
                switch (i) {
                    case 0:
                        model.valueStr = [NSString passNullForString:responseObject[@"masterName"]];
                        break;
                    case 1:
                        model.valueStr = [NSString passNullForString:responseObject[@"sex"]];
                        break;
                    case 2:
                        model.valueStr = [NSString passNullForString:responseObject[@"race"]];
                        break;
                    case 3:
                        model.valueStr = [NSString passNullForString:responseObject[@"birthday"]];
                        break;
                    case 4:
                        model.valueStr = [NSString passNullForString:responseObject[@"education"]];
                        break;
                    case 5:
                        model.valueStr = [NSString passNullForString:responseObject[@"politicsStatus"]];
                        break;
                    case 6:
                        model.valueStr = [NSString passNullForString:responseObject[@"job"]];
                        break;
                    case 7:
                        model.valueStr = [NSString passNullForString:responseObject[@"identity"]];
                        break;
                    case 8:
                        model.valueStr = [NSString passNullForString:responseObject[@"faith"]];
                        break;
                    case 9:
                        model.valueStr = [NSString passNullForString:responseObject[@"health"]];
                        break;
                    case 10:
                        model.valueStr = [NSString passNullForString:responseObject[@"idNumber"]];
                        break;
                    case 11:
                        model.valueStr = [NSString passNullForString:responseObject[@"phone"]];
                        break;
                    case 12:
                        model.valueStr = [NSString passNullForString:responseObject[@"familyNum"]];
                        break;
                    case 13:
                        model.valueStr = [NSString passNullForString:responseObject[@"villageName"]];
                        break;
                    case 14:
                        model.valueStr = [NSString passNullForString:responseObject[@"familyIncome"]];
                        break;
                    case 15:
                        model.valueStr = [NSString passNullForString:responseObject[@"incomeSource"]];
                        break;
                    case 16:
                        model.valueStr = [NSString passNullForString:responseObject[@"floatPeople"]];
                        break;
                    case 17:
                        model.valueStr = [NSString passNullForString:responseObject[@"source"]];
                        break;
                    case 18:
                        model.valueStr = [NSString passNullForString:responseObject[@"homeAddress"]];
                        break;
                        
                    default:
                        break;
                }
            }
            
            NSArray *arr = responseObject[@"familyNums"];
            self.familyPeopleArray = [NSMutableArray array];
            for (int i = 0; i<arr.count; i++) {
                MOMyGetMarriedAddPeopleModel *endModel = [[MOMyGetMarriedAddPeopleModel alloc]init];
                endModel.xingming = [NSString passNullForString:arr[i][@"name"]];
                endModel.xingbie = [NSString passNullForString:arr[i][@"sex"]];
                endModel.chushengnianyue = [NSString passNullForString:arr[i][@"birthday"]];
                endModel.wenhuachengdu = [NSString passNullForString:arr[i][@"education"]];
                endModel.zhengzhimianmao = [NSString passNullForString:arr[i][@"politicsStatus"]];
                endModel.zhiye = [NSString passNullForString:arr[i][@"job"]];
                endModel.shenfen = [NSString passNullForString:arr[i][@"relation"]];
                endModel.jiamkangzhuangkuang = [NSString passNullForString:arr[i][@"health"]];
                endModel.idStr = [NSString passNullForString:arr[i][@"id"]];
                endModel.masterId = [NSString passNullForString:arr[i][@"masterId"]];
                [self.familyPeopleArray addObject:endModel];
            }
            [self.tableView reloadData];
        }
        
    } failure:^(NSError *error) {
        NSLog(@"结亲对象详情：%@",[error localizedDescription]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [[ADShowMessage shareShowMessage]showTextOnly:[error localizedDescription] showInView:self.view];
    }];
}

#pragma mark -- 添加家庭成员
-(void)clickAddFamilyPeopleButton
{
    MOMyGetMarriedAddPeopleView *vc = [[MOMyGetMarriedAddPeopleView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    vc.editOrCreatStatus = @"2";
    vc.clickSureButtonBlock = ^(NSMutableArray * _Nonnull array) {
        
        //NSArray *arr = @[@"户主姓名",@"性别",@"族别",@"出生年月",@"文化程度",@"政治面貌",@"职业",@"身份",@"健康状况"];
        MOMyGetMarriedAddPeopleModel *endModel = [MOMyGetMarriedAddPeopleModel alloc];
        for (int i = 0; i<array.count; i++) {
            MOMyGetMarriedProjectEditOrCreatViewModel *model = array[i];
            switch (i) {
                case 0:
                    endModel.xingming = model.valueStr;
                    break;
                case 1:
                    endModel.shenfen = model.valueStr;
                    break;
                case 2:
                    endModel.xingbie = model.valueStr;
                    break;
                case 3:
                    endModel.chushengnianyue = model.valueStr;
                    break;
                case 4:
                    endModel.wenhuachengdu = model.valueStr;
                    break;
                case 5:
                    endModel.zhengzhimianmao = model.valueStr;
                    break;
                case 6:
                    endModel.zhiye = model.valueStr;
                    break;
                case 7:
                    endModel.jiamkangzhuangkuang = model.valueStr;
                    break;
                    
                default:
                    break;
            }
        }
        [self.familyPeopleArray addObject:endModel];
        [self.tableView reloadData];
        
        if ([self.editOrCreatStatus integerValue]==1) {
            [self editOrCreatPeople:@"2" masterId:self.masterId peopleId:@"" endModel:endModel];
        }
    };
    [self.view addSubview:vc];
}

#pragma mark -- 提交
-(void)clickSubmitButton
{
    NSArray *pArr = @[@"请填写户主姓名",@"请选择性别",@"请填写族别",@"请选择出生年月",@"请填写文化程度",@"请填写政治面貌",@"请填写职业",@"请填写身份",@"请填写宗教信仰",@"请选择健康状况",@"请填写身份证号",@"请填写联系方式",@"请填写家庭人口数量",@"请填写村子名称",@"请填写家庭年收入(万元)",@"请填写主要经济来源",@"请选择是否流动人口",@"请填写流出地",@"请填写家庭住址"];
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:[MOUser sharedMOUser].userName forKey:@"userId"];
    for (int i = 0; i<self.dataSource.count; i++) {
        MOMyGetMarriedProjectEditOrCreatViewModel *model = self.dataSource[i];
        if (model.valueStr.length==0) {
            [[ADShowMessage shareShowMessage]showTextOnly:pArr[i] showInView:self.view];
            return;
        }
        switch (i) {
            case 0:
                [dic setObject:model.valueStr forKey:@"masterName"];
                break;
            case 1:
                [dic setObject:model.valueStr forKey:@"sex"];
                break;
            case 2:
                [dic setObject:model.valueStr forKey:@"race"];
                break;
            case 3:
                [dic setObject:model.valueStr forKey:@"birthday"];
                break;
            case 4:
                [dic setObject:model.valueStr forKey:@"education"];
                break;
            case 5:
                [dic setObject:model.valueStr forKey:@"politicsStatus"];
                break;
            case 6:
                [dic setObject:model.valueStr forKey:@"job"];
                break;
            case 7:
                [dic setObject:model.valueStr forKey:@"identity"];
                break;
            case 8:
                [dic setObject:model.valueStr forKey:@"faith"];
                break;
            case 9:
                [dic setObject:model.valueStr forKey:@"health"];
                break;
            case 10:
                [dic setObject:model.valueStr forKey:@"idNumber"];
                break;
            case 11:
                [dic setObject:model.valueStr forKey:@"phone"];
                break;
            case 12:
                [dic setObject:model.valueStr forKey:@"familyNum"];
                break;
            case 13:
                [dic setObject:model.valueStr forKey:@"villageName"];
                break;
            case 14:
                [dic setObject:model.valueStr forKey:@"familyIncome"];
                break;
            case 15:
                [dic setObject:model.valueStr forKey:@"incomeSource"];
                break;
            case 16:
                [dic setObject:model.valueStr forKey:@"floatPeople"];
                break;
            case 17:
                [dic setObject:model.valueStr forKey:@"source"];
                break;
            case 18:
                [dic setObject:model.valueStr forKey:@"homeAddress"];
                break;
                
            default:
                break;
        }
    }
    if ([self.editOrCreatStatus integerValue]==1) {//编辑
        
    }else{//创建
        if (self.familyPeopleArray.count>0) {
            NSMutableArray *peopleMuArr = [NSMutableArray array];
            for (int i = 0; i<self.familyPeopleArray.count; i++) {
                MOMyGetMarriedAddPeopleModel *model = self.familyPeopleArray[i];
                NSMutableDictionary *muDic = [NSMutableDictionary dictionary];
                [muDic setObject:model.xingming forKey:@"name"];
                [muDic setObject:model.shenfen forKey:@"relation"];
                [muDic setObject:model.xingbie forKey:@"sex"];
                [muDic setObject:model.chushengnianyue forKey:@"birthday"];
                [muDic setObject:model.wenhuachengdu forKey:@"education"];
                [muDic setObject:model.zhengzhimianmao forKey:@"politicsStatus"];
                [muDic setObject:model.zhiye forKey:@"job"];
                [muDic setObject:model.jiamkangzhuangkuang forKey:@"health"];
                [peopleMuArr addObject:muDic];
            }
            [dic setObject:peopleMuArr forKey:@"familyNums"];
        }
    }
    NSLog(@"dic:%@",dic);
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
    NSString * jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    NSLog(@"jsonStr:%@",jsonStr);
    NSString *url = [NSString stringWithFormat:@"%@?authorization=%@",RECIPIENT_ADDRECIPIENT_URL,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkHelper POST:url parameters:@{@"recipientStr":jsonStr} success:^(id responseObject) {
        NSLog(@"结亲对象创建：%@",responseObject);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [[ADShowMessage shareShowMessage]showTextOnly:responseObject[@"msg"] showInView:self.view];
        if ([responseObject[@"flag"] integerValue]==1) {
            [self performSelector:@selector(backAction) withObject:nil afterDelay:1];
        }
    } failure:^(NSError *error) {
        NSLog(@"结亲对象创建：%@",[error localizedDescription]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [[ADShowMessage shareShowMessage]showTextOnly:[error localizedDescription] showInView:self.view];
    }];
    
}

#pragma mark -- 操作完成 返回
-(void)backAction
{
    if (self.editOrCreatSucessBlock) {
        self.editOrCreatSucessBlock();
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -- 编辑状态下 添加编辑家庭成员
-(void)editOrCreatPeople:(NSString *)editOrCreatPeopleStatus masterId:(NSString *)masterId peopleId:(NSString *)peopleId endModel:(MOMyGetMarriedAddPeopleModel *)model
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    if ([editOrCreatPeopleStatus integerValue]==1) {
        [dic setObject:@"edit" forKey:@"type"];
        [dic setObject:masterId forKey:@"masterId"];
        [dic setObject:peopleId forKey:@"id"];
    }else{
        [dic setObject:@"add" forKey:@"type"];
        [dic setObject:masterId forKey:@"masterId"];
    }
    [dic setObject:model.xingming forKey:@"name"];
    [dic setObject:model.shenfen forKey:@"relation"];
    [dic setObject:model.xingbie forKey:@"sex"];
    [dic setObject:model.chushengnianyue forKey:@"birthday"];
    [dic setObject:model.wenhuachengdu forKey:@"education"];
    [dic setObject:model.zhengzhimianmao forKey:@"politicsStatus"];
    [dic setObject:model.zhiye forKey:@"job"];
    [dic setObject:model.jiamkangzhuangkuang forKey:@"health"];
    
    NSString *url = [NSString stringWithFormat:@"%@?authorization=%@",RECIPIENTFAMILY_ADDORUPDATEFAMILY_URL,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkHelper POST:url parameters:dic success:^(id responseObject) {
        NSLog(@"修改/添加家庭成员：%@",responseObject);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [[ADShowMessage shareShowMessage]showTextOnly:responseObject[@"msg"] showInView:self.view];
        
    } failure:^(NSError *error) {
        NSLog(@"修改/添加家庭成员：%@",[error localizedDescription]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [[ADShowMessage shareShowMessage]showTextOnly:[error localizedDescription] showInView:self.view];
    }];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return self.dataSource.count;
    }else{
        return self.familyPeopleArray.count;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        MOMyGetMarriedProjectEditOrCreatViewCell *cell = [MOMyGetMarriedProjectEditOrCreatViewCell cellWithTableView:tableView andIndexPath:indexPath];
        MOMyGetMarriedProjectEditOrCreatViewModel *model = self.dataSource[indexPath.row];
        cell.textFieldEndEditingBlock = ^(NSString * _Nonnull text) {
            model.valueStr = text;
        };
        cell.model = model;
        return cell;
    }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.textLabel.textColor = KBlackColor_222222;
            cell.textLabel.font = FONT_SIZE(15);
        }
        MOMyGetMarriedAddPeopleModel *model = self.familyPeopleArray[indexPath.row];
        cell.textLabel.text = model.xingming;
        
        return cell;
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        MOMyGetMarriedProjectEditOrCreatViewModel *model = self.dataSource[indexPath.row];
        if ([model.layoutType integerValue]==1||[model.layoutType integerValue]==3) {
            return kHConversions(54);
        }else{
            return kHConversions(54) + 16 + 16;
        }
    }else{
        return kHConversions(54);
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section==1) {
        return 13;
    }else{
        return 0.00001;
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return 13;
    }else{
        return kHConversions(54);
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return nil;
    }else{
        UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kHConversions(54))];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, SCREEN_WIDTH, backView.height - 10)];
        label.text = @"家庭成员";
        label.textColor = KBlackColor_222222;
        label.font = [UIFont boldSystemFontOfSize:15];
        [backView addSubview:label];
        
        UIButton *addButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 50, 0, 50, 50)];
        addButton.centerY = label.centerY;
        [addButton setImage:IMAGE(@"use_add") forState:UIControlStateNormal];
        [addButton setImage:IMAGE(@"use_add") forState:UIControlStateHighlighted];
        [addButton addTarget:self action:@selector(clickAddFamilyPeopleButton) forControlEvents:UIControlEventTouchUpInside];
        [backView addSubview:addButton];
        
        return backView;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //if (i==1||i==2||i==3||i==4||i==5||i==8||i==9||i==16) {
    if (indexPath.section==0) {
        MOMyGetMarriedProjectEditOrCreatViewModel *model = self.dataSource[indexPath.row];
        if (indexPath.row==1) {//性别
            SNXYSelectedNumberView *pickview = [[SNXYSelectedNumberView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
            NSArray *tArr = @[@"男",@"女"];
            pickview.numberArray = tArr;
            __weak typeof (self)weakSelf = self;
            pickview.clickIndexBlock = ^(NSInteger index) {
                model.valueStr = tArr[index];
                [weakSelf.tableView reloadData];
            };
            [WINDOW addSubview:pickview];
        }else if (indexPath.row==2){//族别
            SNXYSelectedNumberView *pickview = [[SNXYSelectedNumberView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
            NSArray *tArr = @[@"汉族",@"维吾尔族",@"哈萨克族",@"回族",@"蒙古族",@"柯尔克孜族",@"锡伯族",@"塔吉克族",@"乌孜别克族",@"达斡尔族",@"满族",@"塔塔尔族",@"俄罗斯族"];
            pickview.numberArray = tArr;
            __weak typeof (self)weakSelf = self;
            pickview.clickIndexBlock = ^(NSInteger index) {
                model.valueStr = tArr[index];
                [weakSelf.tableView reloadData];
            };
            [WINDOW addSubview:pickview];
        }else if (indexPath.row==3){//出身年月
            XLsn0wPickerTimer *pickerDate = [[XLsn0wPickerTimer alloc] init];
            pickerDate.maxComponentCount = 3;
            pickerDate.xlsn0wDelegate = self;
            [pickerDate show];
        }else if (indexPath.row==4){//文化程度
            SNXYSelectedNumberView *pickview = [[SNXYSelectedNumberView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
            NSArray *tArr = @[@"小学",@"初中",@"高中",@"大学",@"研究生"];
            pickview.numberArray = tArr;
            __weak typeof (self)weakSelf = self;
            pickview.clickIndexBlock = ^(NSInteger index) {
                model.valueStr = tArr[index];
                [weakSelf.tableView reloadData];
            };
            [WINDOW addSubview:pickview];
        }else if (indexPath.row==5){//政治面貌
            SNXYSelectedNumberView *pickview = [[SNXYSelectedNumberView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
            NSArray *tArr = @[@"党员",@"团员",@"群众"];
            pickview.numberArray = tArr;
            __weak typeof (self)weakSelf = self;
            pickview.clickIndexBlock = ^(NSInteger index) {
                model.valueStr = tArr[index];
                [weakSelf.tableView reloadData];
            };
            [WINDOW addSubview:pickview];
        }else if (indexPath.row==8){//宗教信仰
            SNXYSelectedNumberView *pickview = [[SNXYSelectedNumberView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
            NSArray *tArr = @[@"伊斯兰教",@"佛教",@"基督教",@"无"];
            pickview.numberArray = tArr;
            __weak typeof (self)weakSelf = self;
            pickview.clickIndexBlock = ^(NSInteger index) {
                model.valueStr = tArr[index];
                [weakSelf.tableView reloadData];
            };
            [WINDOW addSubview:pickview];
        }else if (indexPath.row==9){//健康状况
            SNXYSelectedNumberView *pickview = [[SNXYSelectedNumberView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
            NSArray *tArr = @[@"良好",@"一般",@"较差"];
            pickview.numberArray = tArr;
            __weak typeof (self)weakSelf = self;
            pickview.clickIndexBlock = ^(NSInteger index) {
                model.valueStr = tArr[index];
                [weakSelf.tableView reloadData];
            };
            [WINDOW addSubview:pickview];
        }else if (indexPath.row==16){//是否流动
            SNXYSelectedNumberView *pickview = [[SNXYSelectedNumberView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
            NSArray *tArr = @[@"是",@"否"];
            pickview.numberArray = tArr;
            __weak typeof (self)weakSelf = self;
            pickview.clickIndexBlock = ^(NSInteger index) {
                model.valueStr = tArr[index];
                [weakSelf.tableView reloadData];
            };
            [WINDOW addSubview:pickview];
        }
    }
}


- (void)pickerTimer:(XLsn0wPickerTimer *)pickerTimer year:(NSInteger)year month:(NSInteger)month day:(NSInteger)day {
    
    NSString *dateStr = [NSString stringWithFormat:@"%ld-%ld-%ld", (long)year, (long)month, (long)day];
    MOMyGetMarriedProjectEditOrCreatViewModel *model = self.dataSource[3];
    model.valueStr = dateStr;
    [self.tableView reloadData];
}

//1
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
//2
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewCellEditingStyleDelete;
}
//3
//修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}
//4
//点击删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    //在这里实现删除操作
}
//5
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    //删除
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
        NSLog(@"点了：%ld",(long)indexPath.row);
        if ([self.editOrCreatStatus integerValue]==1) {
            MOMyGetMarriedAddPeopleModel *endModel = self.familyPeopleArray[indexPath.row];
           
            NSString *url = [NSString stringWithFormat:@"%@?authorization=%@",RECIPIENTFAMILY_DERECIPIENTFAMILY_URL,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
            
            [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            [PPNetworkHelper POST:url parameters:@{@"id":endModel.idStr} success:^(id responseObject) {
                NSLog(@"删除家庭成员：%@",responseObject);
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                if ([responseObject[@"flag"] integerValue]==1) {
                    [self.familyPeopleArray removeObjectAtIndex:indexPath.row];
                    [self.tableView reloadData];
                }
            } failure:^(NSError *error) {
                NSLog(@"删除家庭成员：%@",[error localizedDescription]);
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                [[ADShowMessage shareShowMessage]showTextOnly:[error localizedDescription] showInView:self.view];
            }];
        }
    }];
    //编辑
    __weak typeof (self)weakSelf = self;
    UITableViewRowAction *editRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"编辑" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        
        NSLog(@"点了：%ld",(long)indexPath.row);
        MOMyGetMarriedAddPeopleView *vc = [[MOMyGetMarriedAddPeopleView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        
        vc.clickSureButtonBlock = ^(NSMutableArray * _Nonnull array) {
            
            MOMyGetMarriedAddPeopleModel *endModel = [MOMyGetMarriedAddPeopleModel alloc];
            for (int i = 0; i<array.count; i++) {
                MOMyGetMarriedProjectEditOrCreatViewModel *model = array[i];
                switch (i) {
                    case 0:
                        endModel.xingming = model.valueStr;
                        break;
                    case 1:
                        endModel.shenfen = model.valueStr;
                        break;
                    case 2:
                        endModel.xingbie = model.valueStr;
                        break;
                    case 3:
                        endModel.chushengnianyue = model.valueStr;
                        break;
                    case 4:
                        endModel.wenhuachengdu = model.valueStr;
                        break;
                    case 5:
                        endModel.zhengzhimianmao = model.valueStr;
                        break;
                    case 6:
                        endModel.zhiye = model.valueStr;
                        break;
                    case 7:
                        endModel.jiamkangzhuangkuang = model.valueStr;
                        break;
                        
                    default:
                        break;
                }
            }
            
            if ([self.editOrCreatStatus integerValue]==1) {
                MOMyGetMarriedAddPeopleModel *m = self.familyPeopleArray[indexPath.row];
                [self editOrCreatPeople:@"1" masterId:m.masterId peopleId:m.idStr endModel:endModel];
            }
            
            [self.familyPeopleArray replaceObjectAtIndex:indexPath.row withObject:endModel];
            [self.tableView reloadData];
        };
        
        vc.endModel = weakSelf.familyPeopleArray[indexPath.row];
        vc.editOrCreatStatus = @"1";
        [WINDOW addSubview:vc];
        
    }];
    editRowAction.backgroundColor = KBlueColor_2C94FF;
    return @[deleteRowAction,editRowAction];
}

#pragma mark - 设置顶头分割线
-(void)viewDidLayoutSubviews {
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        if (@available(iOS 8.0, *)) {
            [self.tableView setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        if (@available(iOS 8.0, *)) {
            [cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
}


-(void)clickBackButton
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -懒加载
-(UIView *)navView
{
    if (!_navView) {
        _navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, StatusBarAndNavigationBarHeight)];
        _navView.backgroundColor = KWhiteColor;
        
        UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(16, 0, 25, 25)];
        [backButton addTarget:self action:@selector(clickBackButton) forControlEvents:UIControlEventTouchUpInside];
        [backButton setImage:IMAGE(@"nav_back") forState:UIControlStateNormal];
        backButton.centerY = StatusBarHeight + (_navView.height - StatusBarHeight)/2;
        [_navView addSubview:backButton];
        
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 32 - 100, 25)];
        titleLabel.text = [self.editOrCreatStatus integerValue]==1?@"编辑结亲对象":@"创建结亲对象";
        titleLabel.font =  [UIFont boldSystemFontOfSize:18];
        titleLabel.textColor = KBlackColor_222222;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.center = CGPointMake(SCREEN_WIDTH/2, backButton.centerY);
        [_navView addSubview:titleLabel];
        
    }
    return _navView;
}

-(UITableView *)tableView
{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, _navView.bottom, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarAndNavigationBarHeight - 57 - TabbarSafeBottomMargin) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = KGrayColor_ededed;
        _tableView.showsVerticalScrollIndicator = NO;
        
    }
    return _tableView;
}

-(UIView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - TabbarSafeBottomMargin - 57, SCREEN_WIDTH, 57)];
        _bottomView.backgroundColor = KWhiteColor;
        
        UIButton *submitButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, _bottomView.width, _bottomView.height)];
        [submitButton setTitle:@"提交" forState:UIControlStateNormal];
        [submitButton setTitleColor:KBlueColor_2C94FF forState:UIControlStateNormal];
        submitButton.titleLabel.font = FONT_SIZE(15);
        [submitButton addTarget:self action:@selector(clickSubmitButton) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:submitButton];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
        lineView.backgroundColor = KGrayColor_ededed;
        [_bottomView addSubview:lineView];
    }
    return _bottomView;
}

@end
